/* eslint-disable comma-dangle */
import type { UserConfig, ConfigEnv, PluginOption } from 'vite'
import { loadEnv } from 'vite'
import { allPlugin } from './vite.plugin'

import { createProxy } from './proxy'
// ts中加载node核心模块需要安装node 的类型补充模块：yarn add -D @types/node
import path from 'path'
// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv): UserConfig => {
  const root = process.cwd()
  const env = loadEnv(mode, root)
  const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY } = env
  const plugins: PluginOption | PluginOption[] = allPlugin(mode)
  return {
    base: VITE_PUBLIC_PATH,
    plugins,
    resolve: {
      alias: {
        '@': path.join(__dirname, 'src'),
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@use "@/assets/styles/variables.scss" as *;',
        },
      },
    },
    define: {
      // 去掉I18N tree shaking 警告
      __VUE_I18N_FULL_INSTALL__: true,
      __VUE_I18N_LEGACY_API__: false,
      __INTLIFY_PROD_DEVTOOLS__: false
    },
    server: {
      // Listening on all local IPs
      host: true,
      open: true, // 浏览器自动打开
      hmr: true, // 热更新
      port: Number(VITE_PORT),
      // Load proxy configuration from .env
      proxy: createProxy(VITE_PROXY),
    },
    build: {
      minify: 'terser',
      terserOptions: {
        compress: {
          keep_infinity: true,
          drop_console: true,
          drop_debugger: true,
        },
      },
      // Turning off brotliSize display can slightly reduce packaging time
      brotliSize: false,
      chunkSizeWarningLimit: 8000,
    },
    optimizeDeps: {
      // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
      include: [
      ],
    },
  }
}
